# -*- coding: utf-8 -*-
# @Time : 2022/2/19 15:31

import os
import json
import time
from Common.mysql import app
from flask import jsonify, request
from src.User.token import token_verify, before_request
from Common.yaml_method import YamlMethod
from src.User.user_info import UserInfo
from src.Case.CaseData.add_case_data import AddCaseData
from src.Case.CaseData.case_data_list import CaseDataList
from src.Case.CaseData.case_tag_list import CaseTagList
from src.Case.CaseData.add_sprint_case import AddSprintCase
from src.Case.CaseData.sprint_case_count import SprintCaseCount
from src.Case.CaseData.export_case import ExportCase
from src.Case.CaseData.import_case import ImportCase

code = YamlMethod().read_data('code.yaml')['code']
evn = YamlMethod().read_data('environment.yaml')['evn']
debug = YamlMethod().read_data('environment.yaml')['debug']


@app.route('/case_data/add', methods=['POST'])
@token_verify
def add_case_data(create_user):
    """
    新增测试用例
    :param create_user: 创建人
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    text = str(data['text'])
    case_name_id = data['name_id']
    get_time = data['get_time']
    update_time = int(time.time() * 1000)

    # 校验参数
    if not all([text, str(case_name_id), create_user]):
        return jsonify(code=code[7], msg="信息不完整")

    # 提交保存测试用例数据
    res = AddCaseData().add_case_data(text, case_name_id, get_time, update_time, create_user)
    return res


@app.route('/case_data/list', methods=['POST'])
@token_verify
def case_data_list(user):
    """
    获取测试用例列表接口
    :return:
    """

    data = json.loads(str(request.data, 'utf-8'))
    case_name_id = data['case_name_id']

    roles = UserInfo().user_info(user)['data']['roles']

    res = CaseDataList().case_data_list(case_name_id)
    res['data']['roles'] = roles
    return res


@app.route('/case_tag/list', methods=['POST'])
@before_request
def case_tag_list():
    """
    获取测试用例标签列表接口
    :return:
    """

    res = CaseTagList().case_tag_list()
    return res


@app.route('/sprint_case/add', methods=['POST'])
@token_verify
def add_sprint_case(create_user):
    """
    筛选版本测试用例接口
    :return:
    """

    data = json.loads(str(request.data, 'utf-8'))
    case_name_id = data['nameId']
    name_id_list = data['submitCaseNameId']
    level_list = data['levelLabel']
    tag_list = data['submitCaselabel']

    # 校验参数
    if not all([str(case_name_id), name_id_list, create_user]):
        return jsonify(code=code[7], msg="信息不完整")

    res = AddSprintCase().add_sprint_case(name_id_list, case_name_id, create_user, level_list, tag_list)
    return res


@app.route('/sprint_case/count', methods=['POST'])
@before_request
def sprint_case_count():
    """
    统计测试用例执行结果接口
    :return:
    """

    if debug == 'vue':
        data = json.loads(str(request.data, 'utf-8'))
        case_name_id = data['case_name_id']
    else:
        case_name_id = int(request.form.get('case_name_id'))

    res = SprintCaseCount().sprint_case_count(case_name_id)
    return res


@app.route('/case_data/export', methods=['POST'])
@before_request
def export_case():
    """
    导出测试用例接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    case_name_id = data['name_id']

    res = ExportCase().export_case(case_name_id)
    return res


@app.route('/case_data/import', methods=['POST'])
@token_verify
def parsing_case(create_user):
    """
    解析导入的测试用例测试用例接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    case_name_id = data['case_name_id']

    res = ImportCase().import_case(str(case_name_id)+'.xmind', case_name_id, create_user)
    return res
